<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       table{
        border-collapse: collapse;
        }
        button{
            margin-left: 10px;
        } 
    </style>
</head>

<body>
    <button id="addBtn">添加一条新的访客信息</button>
    <button id="delAllBtn">删除选中</button>
    <button id="ageBtn">年龄从小到大</button>
    <!-- 序列号	姓名	年龄	操作 -->

    <table border=1 cellspacing=0 cellpadding=10>
        <tbody>
            <tr>
                <th>序列号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>操作</th>
                <th>选中</th>
            </tr>

            <!--
            <tr>
                <td>1</td>
                <td>胡杭</td>
                <td>16</td>
                <td><button>删除</button></td>
            </tr>
            -->
        </tbody>
    </table>


    <script>
        var info = [{
            name: "胡杭",
            age: 16
        }, {
            name: "胜明",
            age: 22
        }, {
            name: "军毅",
            age: 21
        }, {
            name: "晓华",
            age: 13
        }, {
            name: "盛聪",
            age: 23
        }, {
            name: "侦剑",
            age: 32
        }, {
            name: "红翔",
            age: 25
        }, {
            name: "超维",
            age: 18
        }, {
            name: "士琪",
            age: 22
        }, {
            name: "艳华",
            age: 20
        }];

        let tbody = document.querySelector("tbody");
        let addBtn = document.getElementById("addBtn");
        let ageBtn = document.getElementById("ageBtn");
        let delAllBtn = document.getElementById("delAllBtn");
        let delBtn = tbody.querySelectorAll("button");
        
        // let delBtn = tbody.getElementsByTagName("button");

        let index = 0; // 模拟点击下标(每次自增1)
        //添加内容，以及删除内容
        addBtn.onclick = function () {
            console.log(index);
            // 当前index值要小于数组长度,否则会报错，获取不到数组的值
            if (index < info.length) {
                // 创建新的tr
                let newTr = document.createElement("tr");
                // 新的tr添加tr单元格
                newTr.innerHTML = `
                <td>${index + 1}</td>
                <td>${info[index].name}</td>
                <td>${info[index].age}</td>
                <td><button>删除</button></td>
                <td><input type="checkbox" name="选择" id=""></td>
                `;
                //  将新的tr添加到tbody
                tbody.appendChild(newTr);

                // 删除单个信息
                delBtn = tbody.querySelectorAll("button");
                // console.log(delBtn);
                // 每个删除按钮绑定点击事件
                delBtn.forEach(function (el) {
                    el.onclick = function () {
                        // el == this
                        // console.log(this.parentElement.parentElement.remove());
                        el.parentElement.parentElement.remove();
                    }
                });
                // console.log("before", delBtn);
                // for (let i = 0; i < delBtn.length; i++) {
                //     delBtn[i].onclick = function () {
                //         delBtn[i].parentElement.parentElement.remove();
                //         console.log("after", delBtn);
                //     }
                // }

                // 添加完毕后自增
                index++;
            }
        };
        //delAllBtn
        delAllBtn.onclick = function () {
            sle=tbody.querySelectorAll("input[type='checkbox']");

             for(let i=0;i<sle.length+1;i++){
                sle[i].parentElement.parentElement.remove();
             }
        };
        // 年龄大小排序
        ageBtn.onclick = function () {
            ageAll=tbody.querySelectorAll("tr:not(first-child)");
            agee=tbody.querySelectorAll("td:nth-child(3)");
            console.log(agee);
            console.log(ageAll);
            ageAll_arr=[];
            for(let i =0;i<ageAll.length;i++){
                ageAll_arr.push(ageAll[i]);
            }
            ageAll_arr.sort(function(a,b){
                // console.log(b);
                /* console.log(a.cells[2]); */
                return a.cells[2].innerText-b.cells[2].innerText;

            })
            for(let i = 0; i < ageAll_arr.length; i++) {
            tbody.appendChild(ageAll_arr[i]);
            }
        };
    </script>
</body>

</html>